home *** CD-ROM | disk | FTP | other *** search
/ Tools Unlimited 1: The Blanker Collection / Tools Unlimited - Vol 1 - The Blanker Collection.iso / Blanker / Packages / GarshneBlanker / GSource / Blankers / main.c < prev    next >
C/C++ Source or Header  |  1996-01-25  |  7KB  |  336 lines

  1. /*
  2.  *    Copyright (c) 1994 Michael D. Bayne.
  3.  *    All rights reserved.
  4.  *
  5.  *    Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/memory.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <graphics/gfxbase.h>
  12. #include <string.h>
  13.  
  14. #include "includes.h"
  15.  
  16. LONG PortRemoved = FALSE, Blanking = FALSE, *ServerBlanking = 0L;
  17. ULONG InitSecs, InitMicros, CheckCPU = FALSE, CheckCPUDisabled = FALSE;
  18. struct Library *IntuitionBase, *GarshnelibBase, *GfxBase;
  19. BYTE PortName[] = "GarshneClient";
  20. struct MsgPort *ClientPort, *TimerPort;
  21. struct timerequest *TimeOutIO;
  22. PrefObject *CurPrefs;
  23. BYTE PrefsPath[128];
  24.  
  25. extern __far LONG RangeSeed;
  26.  
  27. PrefObject *LoadPrefs( STRPTR Path )
  28. {
  29.     PrefObject *Prefs;
  30.     BPTR PrefFile;
  31.     LONG Objects;
  32.  
  33.     if( Prefs = AllocVec( sizeof( PrefObject ) * 25, MEMF_CLEAR ))
  34.     {
  35.         if( PrefFile = Open( Path, MODE_OLDFILE ))
  36.         {
  37.             Read( PrefFile, &Objects, sizeof( LONG ));
  38.             Read( PrefFile, Prefs, sizeof( PrefObject ) * Objects );
  39.             Close( PrefFile );
  40.         }
  41.         else
  42.             Defaults( Prefs );
  43.     }
  44.  
  45.     return Prefs;
  46. }
  47.  
  48. int main( void )
  49. {
  50.     struct Process *Proc = ( struct Process * )FindTask( 0L );
  51.     BlankMsg *FreeMsg;
  52.     LONG ReturnVal = 0;
  53.     
  54.     if( FindPort( PortName ))
  55.         return 1;
  56.     
  57.     if( Proc->pr_CLI )
  58.     {
  59.         STRPTR Str = BADDR((( struct CommandLineInterface * )
  60.                             BADDR( Proc->pr_CLI ))->cli_CommandName );
  61.         CopyMem( Str + 1, PrefsPath, *Str );
  62.         PrefsPath[*Str] = '\0';
  63.         strcat( PrefsPath, ".prefs" );
  64.     }
  65.     
  66.     IntuitionBase = OpenLibrary( "intuition.library", 37L );
  67.     GfxBase = OpenLibrary( GRAPHICSNAME, 37L );
  68.     GarshnelibBase = OpenLibrary( "Garshnelib.library", 37L );
  69.     
  70.     if( IntuitionBase && GfxBase && GarshnelibBase )
  71.     {
  72.         ClientPort = CreateMsgPort();
  73.         TimerPort = CreateMsgPort();
  74.         
  75.         if( ClientPort && TimerPort )
  76.         {
  77.             TimeOutIO = ( struct timerequest * )
  78.                 CreateExtIO( TimerPort, sizeof( struct timerequest ));
  79.             
  80.             if( TimeOutIO && !OpenDevice( "timer.device", UNIT_VBLANK,
  81.                                          ( struct IORequest * )TimeOutIO, 0L ))
  82.             {
  83.                 ClientPort->mp_Node.ln_Name = PortName;
  84.                 ClientPort->mp_Node.ln_Pri = 0L;
  85.                 AddPort( ClientPort );
  86.                 CurPrefs = LoadPrefs( PrefsPath );
  87.                 
  88.                 CurrentTime( &InitSecs, &InitMicros );
  89.                 RangeSeed = InitSecs + InitMicros;
  90.                 
  91.                 if( MessageServer( BM_INITMSG ) == OK )
  92.                 {
  93.                     LONG Sigs, Ret;
  94.                     
  95.                     do
  96.                     {
  97.                         Sigs = Wait( SIG_PORT | SIGBREAKF_CTRL_C );
  98.                         Ret = HandleSignal( Sigs );
  99.                     }
  100.                     while( Ret == OK );
  101.                 }
  102.                 else
  103.                 {
  104.                     Complain( "Init message to server failed." );
  105.                     ReturnVal = 3;
  106.                 }
  107.                 
  108.                 while( FreeMsg = ( BlankMsg * )GetMsg( ClientPort ))
  109.                 {
  110.                     if( FreeMsg->bm_Type & BF_REPLY )
  111.                         FreeVec( FreeMsg );
  112.                     else
  113.                     {
  114.                         FreeMsg->bm_Type |= BF_REPLY;
  115.                         ReplyMsg(( struct Message * )FreeMsg );
  116.                     }
  117.                 }
  118.                 
  119.                 if( CurPrefs )
  120.                     FreeVec( CurPrefs );
  121.             
  122.                 if( !PortRemoved )
  123.                     RemPort( ClientPort );
  124.             }
  125.             
  126.             if( TimeOutIO )
  127.             {
  128.                 if( TimeOutIO->tr_node.io_Device )
  129.                     CloseDevice(( struct IORequest * )TimeOutIO );
  130.                 DeleteExtIO(( struct IORequest * )TimeOutIO );
  131.             }
  132.         }
  133.         else
  134.         {
  135.             Complain( "ClientPort or TimerPort failed to open." );
  136.             ReturnVal = 2;
  137.         }
  138.  
  139.         if( ClientPort )
  140.         {
  141.             BlankMsg *TmpMsg;
  142.             while( TmpMsg = ( BlankMsg * )GetMsg( ClientPort ))
  143.             {
  144.                 TmpMsg->bm_Flags |= BF_REPLY;
  145.                 ReplyMsg(( struct Message * )TmpMsg );
  146.             }
  147.             DeleteMsgPort( ClientPort );
  148.         }
  149.  
  150.         if( TimerPort )
  151.             DeleteMsgPort( TimerPort );
  152.     }
  153.     else
  154.     {
  155.         Complain( "A library failed to open." );
  156.         ReturnVal = 1;
  157.     }
  158.     
  159.     if( GarshnelibBase )
  160.         CloseLibrary( GarshnelibBase );
  161.     
  162.     if( GfxBase )
  163.         CloseLibrary( GfxBase );
  164.     
  165.     if( IntuitionBase )
  166.         CloseLibrary( IntuitionBase );
  167.     
  168.     return ReturnVal;
  169. }
  170.  
  171. LONG MessageServer( LONG Type )
  172. {
  173.     struct MsgPort *ServerPort;
  174.     BlankMsg *ClientMsg;
  175.     
  176.     if( ServerPort = FindPort( "GarshneServer" ))
  177.     {
  178.         if( ClientMsg = AllocVec( sizeof( BlankMsg ), MEMF_PUBLIC|MEMF_CLEAR ))
  179.         {
  180.             ClientMsg->bm_Mess.mn_ReplyPort = ClientPort;
  181.             ClientMsg->bm_Mess.mn_Length = sizeof( BlankMsg );
  182.             ClientMsg->bm_Type = Type;
  183.             PutMsg( ServerPort, ( struct Message * )ClientMsg );
  184.             
  185.             return OK;
  186.         }
  187.     }
  188.     
  189.     return QUIT;
  190. }
  191.  
  192. LONG HandleSignal( LONG Signal )
  193. {
  194.     BlankMsg *CurMsg;
  195.     LONG RetVal = OK;
  196.     
  197.     if( Signal & SIG_TIMER )
  198.     {
  199.         MessageServer( BM_FAILED );
  200.         RetVal = UNBLANK;
  201.     }
  202.     
  203.     if( Signal & SIG_PORT )
  204.     {
  205.         while( CurMsg = ( BlankMsg * )GetMsg( ClientPort ))
  206.         {
  207.             LONG Type = CurMsg->bm_Type;
  208.             LONG Flags = CurMsg->bm_Flags;
  209.             
  210.             if( Type != BM_PING )
  211.                 ServerBlanking = ( LONG * )CurMsg->bm_Mess.mn_Node.ln_Name;
  212.             
  213.             if( Flags & BF_REPLY )
  214.                 FreeVec( CurMsg );
  215.             else
  216.             {
  217.                 CurMsg->bm_Flags |= BF_REPLY;
  218.                 ReplyMsg(( struct Message * )CurMsg );
  219.             }
  220.             
  221.             switch( Type )
  222.             {
  223.             case BM_DOBLANK:
  224.             case BM_DOTESTBLANK:
  225.                 if( !Blanking )
  226.                 {
  227.                     PrefObject *TmpPrefs;
  228.  
  229.                     Blanking = TRUE;
  230.                     CurrentTime( &InitSecs, &InitMicros );
  231.                     if( Type == BM_DOTESTBLANK )
  232.                         TmpPrefs = LoadPrefs( "T:GBlankerTmpPrefs" );
  233.                     switch( Blank( Type == BM_DOBLANK ? CurPrefs : TmpPrefs ))
  234.                     {
  235.                     case FAILED:
  236.                         /* In this case the Blank() function failed to init */
  237.                         /* (ie. no memory or something). So we yell. */
  238.                         MessageServer( BM_FAILED );
  239.                         break;
  240.                     case DELAYEDQUIT:
  241.                     case QUIT:
  242.                         RetVal = QUIT;
  243.                         break;
  244.                     }
  245.                     Blanking = FALSE;
  246.                     if( Type == BM_DOTESTBLANK )
  247.                         FreeVec( TmpPrefs );
  248.                 }
  249.                 break;
  250.             case BM_RELOADPREFS:
  251.                 if( CurPrefs )
  252.                     FreeVec( CurPrefs );
  253.                 CurPrefs = LoadPrefs( PrefsPath );
  254.                 break;
  255.             case BM_DELAYEDQUIT:
  256.                 RemPort( ClientPort );
  257.                 PortRemoved = TRUE;
  258.                 RetVal = DELAYEDQUIT;
  259.                 break;
  260.             case BM_DOQUIT:
  261.                 RetVal = QUIT;
  262.                 break;
  263.             case BM_UNBLANK:
  264.                 break;
  265.             }
  266.         }
  267.     }
  268.     
  269.     if( Signal & SIGBREAKF_CTRL_C )
  270.         RetVal = QUIT;
  271.  
  272.     return RetVal;
  273. }
  274.  
  275. LONG ContinueBlanking( VOID )
  276. {
  277.     LONG RetVal;
  278.  
  279.     if( !CheckCPUDisabled )
  280.     {
  281.         if( !CheckCPU )
  282.         {
  283.             ULONG Secs, Micros;
  284.             
  285.             CurrentTime( &Secs, &Micros );
  286.             if( Secs - InitSecs > 3 )
  287.                 CheckCPU = TRUE;
  288.         }
  289.         
  290.         TimeOutIO->tr_node.io_Command = TR_ADDREQUEST;
  291.         TimeOutIO->tr_time.tv_secs = ( CheckCPU ? 1 : 5 );
  292.         SendIO(( struct IORequest * )TimeOutIO );
  293.     }
  294.     
  295.     RetVal = HandleSignal( Wait( SIG_TIMER | SIG_PORT | SIGBREAKF_CTRL_C ));
  296.  
  297.     if( !CheckCPUDisabled )
  298.     {
  299.         AbortIO(( struct IORequest * )TimeOutIO );
  300.         WaitIO(( struct IORequest * )TimeOutIO );
  301.         SetSignal( 0L, SIG_TIMER );
  302.     }
  303.  
  304.     switch( RetVal )
  305.     {
  306.     case OK:
  307.         if( !*ServerBlanking )
  308.             RetVal = UNBLANK;
  309.         break;
  310.     case DELAYEDQUIT:
  311.         /* Lower our priority so our quitting doesn't interfere with the new 
  312.            blanker. */
  313.         Delay( 60 );
  314.         break;
  315.     }
  316.  
  317.     if( !CheckCPUDisabled )
  318.     {
  319.         if( RetVal == UNBLANK )
  320.             CheckCPU = FALSE;
  321.     }
  322.  
  323.     return RetVal;
  324. }
  325.  
  326. VOID Complain( STRPTR Bitch )
  327. {
  328.     if( IntuitionBase )
  329.     {
  330.         struct EasyStruct ErrorReq = {
  331.             sizeof( struct EasyStruct ), 0, "Error", 0L, "Ok" };
  332.         ErrorReq.es_TextFormat = Bitch;
  333.         EasyRequestArgs( 0L, &ErrorReq, 0L, 0L );
  334.     }
  335. }
  336.